/**
 * Created by Ren on 2015/6/3.
 */
function Player(ctx, fireAudio, bullets, playerAudio, bulletAudio)
{
    this.bulletAudio = bulletAudio;
    this.img = new Image();
    this.img.src = 'images/Player.png';
    this.xltImg = new Image();
    this.xltImg.src = 'images/xlt.png';
    this.x = 250;
    this.y = 450;
    this.exploded = false;
    this.explodedImg = new Image();
    this.explodedImg.src = 'images/explosionPlayer.png';
    this.ctx = ctx;
    this.playerSpeed = 4;
    this.bulSpeed = 5;
    this.flag = false;
    this.playerLife = 100;
    this.maxLife = 200;
    this.fireTime = 0;
    this.isFire = true;
    this.isShow = false;
    this.fireAudio = fireAudio;
    this.playerAudio = playerAudio;
    this.explodedIndex = 0;
    this.bullets = bullets;
    this.score = 0;
    this.width = 50;
    this.mubiao = 5000;
    this.height = 50;
}
Player.prototype.draw = function()
{
    if(gameStatus.game_Status == 1)
    {
        if(this.flag)
        {
            this.flag = false;
            this.exploded = false;
            this.x = 250;
            this.y = 400;
            this.playerLife = 100;
            this.explodedIndex = 0;
        }
        if(!this.exploded)
        {
            if(this.isShow)
            {
                this.explosion();
            }
            if(keyStatus.KEY_UP_Status)
            {
                this.y -= this.playerSpeed;
            }
            if(keyStatus.KEY_DOWN_Status)
            {
                this.y += this.playerSpeed;
            }
            if(keyStatus.KEY_RIGHT_Status)
            {
                this.x += this.playerSpeed;
            }
            if(keyStatus.KEY_LEFT_Status)
            {
                this.x -= this.playerSpeed;
            }
            if(this.x < 0)
            {
                this.x = 0;
            }
            if(this.x > 550)
            {
                this.x = 550
            }
            if(this.y < 0)
            {
                this.y = 0;
            }
            if(this.y > 400)
            {
                this.y = 400;
            }
            if(keyStatus.KEY_SPACE_Status)
            {
                if(++this.fireTime > 15)
                {
                    this.isFire = true;
                }
                if(this.isFire)
                {
                    this.fire();
                    if(this.fireAudio)
                    {
                        this.fireAudio.currentTime = 0;
                        this.fireAudio.pause();
                        this.fireAudio.play();
                    }
                    this.isFire = false;
                    this.fireTime = 0;
                }
            }
            if(this.playerLife < 100)
            {
                this.playerLife += 0.0005;
            }
            if(this.playerLife > this.maxLife)
            {
                this.playerLife = 100;
            }
            if(this.score > this.mubiao)
            {
                gameStatus.game_isBoss = true;
                this.playerLife += 25;
                this.mubiao = 1.5*this.mubiao;
            }
            this.ctx.fillStyle = "gray";
            this.ctx.font = '20px 宋体';
            this.ctx.fillText('Score:   '+this.score, 400, 50);
            if(this.playerLife >  60)
            {
                this.ctx.drawImage(this.xltImg, 0, 0, 55, 20, 600 - this.playerLife*2, 0, this.playerLife*2, 25);
            }
            else if(this.playerLife > 30)
            {
                this.ctx.drawImage(this.xltImg, 70, 0, 55, 20, 600 - this.playerLife*2, 0, this.playerLife*2, 25);
            }
            else
            {
                this.ctx.drawImage(this.xltImg, 140, 0, 55, 20, 600 - this.playerLife*2, 0, this.playerLife*2, 25);
            }
            this.ctx.drawImage(this.img, 0, 0, 100, 100, this.x, this.y, this.width, this.height);
        }
        else
        {
            this.explosion();
            if(!this.flag)
            {
                gameStatus.game_Status = 2;
                keyStatus.KEY_ANY_Status = false;
                this.flag = true;
            }
        }
    }
    else
    {
        this.bullets.removeALL();
    }
}
Player.prototype.getCenter=function() {
    return new Point(this.x + this.width/2, this.y + this.height/2);
}
Player.prototype.fire = function()
{
    var bullet1 = new Bullet(this.ctx, this.x - 10, this.y, this.bullets, this.bulSpeed, 85, "images/projectile.png", this.bulletAudio, -30*Math.PI/180, 16, 28);
    var bullet6 = new Bullet(this.ctx, this.x -5, this.y, this.bullets, this.bulSpeed, 80, "images/projectile.png", this.bulletAudio, -10*Math.PI/180, 16, 28);
    var bullet2 = new Bullet(this.ctx, this.x, this.y, this.bullets, this.bulSpeed, 80, "images/projectile.png", this.bulletAudio, 0, 16, 28);
    var bullet7 = new Bullet(this.ctx, this.x + 5, this.y, this.bullets, this.bulSpeed, 80, "images/projectile.png", this.bulletAudio, 10*Math.PI/180, 16, 28);
    var bullet3 = new Bullet(this.ctx, this.x + 10, this.y, this.bullets, this.bulSpeed, 84, "images/projectile.png", this.bulletAudio, 30*Math.PI/180, 16, 28);
    var bullet4 = new Bullet(this.ctx, this.x + 10, this.y + 50, this.bullets, this.bulSpeed, 8, "images/projectile.png", this.bulletAudio, 150*Math.PI/180, 16, 28);
    var bullet5 = new Bullet(this.ctx, this.x - 10, this.y + 50, this.bullets, this.bulSpeed, 8, "images/projectile.png", this.bulletAudio, 210*Math.PI/180, 16, 28);
    this.bullets.push(bullet1);
    this.bullets.push(bullet2);
    this.bullets.push(bullet3);
    this.bullets.push(bullet4);
    this.bullets.push(bullet5);
    this.bullets.push(bullet6);
    this.bullets.push(bullet7);
}
Player.prototype.explosion=function() {
    if(this.exploded)
    {
        if(this.playerAudio && this.explodedIndex < 7)
        {
            this.playerAudio.currentTime = 0;
            this.playerAudio.pause();
            this.ctx.drawImage(this.explodedImg,this.explodedIndex*42, 0, 42, 45, this.x,this.y, 42, 45);
            this.explodedIndex ++;
            this.playerAudio.play();
        }
    }
    else
    {
        if(this.explodedIndex < 4)
        {
            this.ctx.drawImage(this.explodedImg,this.explodedIndex*42, 0, 42, 45, this.x,this.y, 42, 45);
            this.explodedIndex ++;
        }
        this.isShow = false;
        this.explodedIndex = 0;
    }
}